<template>
  <div class="flex h-full">
    <a-card class="mr-2" :body-style="{ height: '100%', display: 'flex', flexDirection: 'column' }">
      <PatientListDrawer :patient-list="patientList" :selected-item="selectedItem" :loading="loading" @select="selectPatient" @refresh="getInpRecordList">
        <template #search>
          <SelectArea
            v-model:value="searchParams.bqid"
            use-cache
            post-url
            :post-params="{ wardTypeCodes: ['QYLB001', 'QYLB002'] }"
            @change="getInpRecordList()"
          />
        </template>
      </PatientListDrawer>
    </a-card>
    <div class="flex flex-col flex-1 overflow-hidden">
      <PatientInfoHeader :patient-info="selectedItem">
        <template #prefix>
          <div class="text-2xl pr-4 text-primary font-semibold">{{ selectedItem.bedCode }}床</div>
        </template>
        <a-divider type="vertical" :style="{ height: '40px', margin: '0 16px' }" />
        <div>
          <div class="flex items-center">
            <LabelValue label="住院科室" :width="190" :value="selectedItem.ksmc" />
            <LabelValue label="住院号" :width="210" :value="selectedItem.admissionCode" />
            <LabelValue label="入区时间" :width="230" :value="selectedItem.lastDeptRegTime" />
            <!-- <LabelValue label="过敏史" :width="200" tooltip value-class="text-red-600" :value="selectedItem.brGms" /> -->
            <AllergyHistory
              :width="200"
              value-class="text-red-600"
              :value="selectedItem.brGms"
              :patient-info="selectedItem"
              @success="(e) => (selectedItem.brGms = e)"
            />
          </div>
          <div class="flex items-center">
            <LabelValue label="住院病区" :width="190" :value="selectedItem.wardName" />
            <LabelValue label="账户余额" :width="210" :value="formatAmount(selectedItem.yjje) + '元'" />
            <LabelValue label="入院时间" :width="230" :value="selectedItem.admissionTime" />
            <LabelValue label="入院诊断" :width="200" tooltip :value="selectedItem.ryzd" />
          </div>
        </div>
      </PatientInfoHeader>
      <div class="flex flex-1 mt-2 overflow-hidden" :class="{ 'disabled-style': loading || !selectedItem.inplsh }">
        <a-card class="flex-1 overflow-hidden" :body-style="{ padding: '8px', height: '100%', overflow: 'auto' }">
          <Order :id="selectedItem.inplsh" />
        </a-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { useRoute } from 'vue-router'
import PatientInfoHeader from '@/components/business/PatientInfoHeader'
import { formatAmount } from '@/utils/format'
import Order from './Order'

const route = useRoute()
const patientList = ref([])
const selectedItem = ref({})
const loading = ref(true)
const searchParams = ref({
  bqid: ''
})
// 获取左侧患者列表
const getInpRecordList = async () => {
  loading.value = true
  selectedItem.value = {} // 清空选中的患者
  const { code, data, msg } = await post('/inp/inpComprehensive/getInpRecordInfo', {
    bqid: searchParams.value.bqid
  })
  loading.value = false
  if (code !== 0) return message.error(msg)
  patientList.value = data
  if (data && data.length > 0) {
    if (route.query?.inplsh) {
      selectedItem.value = data.find((item) => item.inplsh === route.query?.inplsh) || {}
    } else {
      selectedItem.value = data[0]
    }
  }
}
const selectPatient = (patient) => {
  selectedItem.value = patient
}
onMounted(() => {
  getInpRecordList()
})
</script>
<style lang="less" scoped></style>
